package com.csmk.springboot_simple.exception;

import com.csmk.springboot_simple.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ServiceException.class)
    public R exceptionHandler(ServiceException ex){
        log.error(ex.getMessage());
        return R.error(ex.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object notValidException(HttpServletRequest request, MethodArgumentNotValidException e) {
        log.info("请求的url为{}出现数据校验异常,异常信息为:", request.getRequestURI(), e);
        return R.error(e.getMessage());
    }


}
